/**
 * SystemAdmin.java
 * @author Peter
 */
package edu.gatech.coc.betameisters.people;

import java.util.LinkedList;
import java.util.List;

import edu.gatech.coc.betameisters.infoholders.Constants.UserType;
import edu.gatech.coc.betameisters.utilities.Database;
import edu.gatech.coc.betameisters.utilities.Security;

/**
 * The System Admin class; the person who has the priorities to do stuff within
 * the system
 * 
 * @author Peter
 * 
 * @version $Revision: 1.0 $
 */
public class SystemAdmin extends Person {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5936041236565127986L;

	/**
	 * Field current
	 */
	private Person current;

	/**
	 * Field employeeNameDB - get from database
	 */
	private final Security securityInt;

	/**
	 * Constructor that creates a system admin <!-- // $codepro.audit.disable
	 * spellCheckComments -->
	 * 
	 * @param fName
	 *            - system admin's first name
	 * @param lName
	 *            - system admin's last name
	 */
	public SystemAdmin(String fName, String lName) {
		super(fName, lName);
		securityInt = new Security();
		personType = UserType.ADMIN;
		MYLOGGER.info("In SystemAdmin constructor");
	}
	
	/**
	 * Default constructor for an admin
	 */
	public SystemAdmin(){
		super(); // $codepro.audit.disable invocationOfDefaultConstructor
		securityInt = new Security();
		personType = UserType.ADMIN;
		MYLOGGER.info("In SystemAdmin constructor");
	}
	
	/**
	 * Database constructor for an admin
	 * @param username Username 
	 * @param encryptPass password
	 * @param firstName first name
	 * @param lastName last name
	 */
	public SystemAdmin(String username, String encryptPass, String firstName,
			String lastName) {
		super(username, encryptPass, firstName, lastName);
		securityInt = new Security();
		personType = UserType.ADMIN;
		MYLOGGER.info("In SystemAdmin constructor");
	}

	/**
	 * Resets the user's password
	 * 
	 * @return the user's new password
	 */
	public String resetPassword() {
		MYLOGGER.info("System Admin resetting user's password");
		return securityInt.resetPassword(current);
	}

	/**
	 * Modify user details
	 */
	public void modifyUser() {
		MYLOGGER.info("System Admin is trying to modify user details");
		// pulls up on UI choices to make changes to
	}

	/**
	 * Gets the current person.
	 * 
	 * @return the current person.
	 */
	public Person getCurrent() {
		return current;
	}

	/**
	 * Sets the current person.
	 * 
	 * @param someone
	 *            the new field person
	 */
	public void setCurrent(Person someone) {
		current = someone;
	}

	/**
	 * Overrides the default toString method
	 * 
	 * @return System Admin's name
	 */
	public String toString() {
		return "System Admin: " + this.getFirstName() + " "
				+ this.getLastName();
	}

	/**
	 * Search for a user
	 * @param uName user name or first name/ last name
	 * @param type Type of user
	 * @param isUserName if true, it will treat input as a username
	 * @return a list of usernames
	 */
	public List<String> searchUser(final String uName, final UserType type, // $codepro.audit.disable
			final boolean isUserName) {
		// If they gave us a firstname/lastname
		if(!isUserName){
			final char[] split = uName.toCharArray();
			String fName = "", lName = "";
			for(int i = 0; i < uName.length(); i++){
				if(Character.toString(split[i]).equals(" ")){
					fName = lName;
					lName = "";
				}
				else{
					lName += split[i]; // $codepro.audit.disable stringConcatenationInLoop
				}
			}
			final List<Person> personList = Database.searchUsersByName(fName, lName);
			final List<String> nameList = new LinkedList<String>();
			MYLOGGER.info("System Admin is trying to search for user");
			
			// copy over all the user ids
			for(Person p : personList) {
				nameList.add(p.getUserID());
			}
			// return the user ids
			return nameList;
		}
		// they gave us a username
		else {
			final List<String> nameList = new LinkedList<String>();
			
			final Patient pat = Database.loadPatient(uName);
			final Nurse nur = Database.loadNurse(uName);
			final Doctor doc = Database.loadDoctor(uName);
			final SystemAdmin adm = Database.loadSystemAdmin(uName);
			
			if(pat != null) {
				current = pat;
				nameList.add(pat.getUserID());
			}
			else if(nur != null) {
				current = nur;
				nameList.add(nur.getUserID());
			}
			else if(doc != null) {
				current = doc;
				nameList.add(doc.getUserID());
			}
			else if(adm != null) {
				current = adm;
				nameList.add(adm.getUserID());
			}
			return nameList;
		}
	}

	/**
	 * Delete a user from system
	 * 
	 * @param uName
	 * @param type
	 * @return true if user is deleted, false otherwise
	 */
	public boolean deleteUser(final String uName, final UserType type) { // $codepro.audit.disable
																			// booleanMethodNamingConvention
		MYLOGGER.info("System Admin is trying to delete an account for user");
		return Database.deleteUser(uName);
	}
	
	/**
	 * Create a user with an assigned type
	 * 
	 * @param fName First name
	 * @param lName Last name
	 * @param type user type
	 * @return true if user was created, false otherwise
	 */
	@SuppressWarnings("unused")
	public boolean createUser(String fName, String lName, UserType type) { // $codepro.audit.disable
		MYLOGGER.info("System Admin is trying to create an account for user");
		switch (type) {
		case PATIENT:	
			final Patient pat = new Patient(fName, lName); // $codepro.audit.disable variableUsage
			return true;
		case NURSE:
			final Nurse aNurse = new Nurse(fName, lName); // $codepro.audit.disable variableUsage
			return true;
		case DOCTOR:
			final Doctor aDoctor = new Doctor(fName, lName); // $codepro.audit.disable variableUsage
			return true;
		case ADMIN:
			final SystemAdmin anAdmin = new SystemAdmin(fName, lName); // $codepro.audit.disable variableUsage
			return true;
		default:
			break;
		}
		return false;
	}
}
